﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using FrbaBus.Properties;

namespace FrbaBus.ConsultaPuntos
{
    public partial class FormConsultarPuntos : Form
    {
        public FormConsultarPuntos()
        {
            InitializeComponent();
            
            grillaPuntos.Columns.Add("coit_tipo", "TIPO DE COMPRA");
            grillaPuntos.Columns.Add("punt_cantidad_total", "PUNTOS");
            grillaPuntos.Columns.Add("punt_cant_usada", "PUNTOS UTILIZADOS");
            grillaPuntos.Columns.Add("punt_fecha", "FECHA");
            grillaPuntos.Columns.Add("punt_fecha_venc", "VENCIMIENTO");

            grillaCanjes.Columns.Add("can_fecha", "FECHA");
            grillaCanjes.Columns.Add("prem_nombre", "PREMIO");
            grillaCanjes.Columns.Add("can_cant", "CANTIDAD");
            grillaCanjes.Columns.Add("prem_precio", "PUNTOS UTILIZADOS");
            
            Cargar_Puntos();

            Cargar_Grilla_Detalle_Puntos();

            Cargar_Grilla_Detalle_Canjes();
        }

        private void Cargar_Grilla_Detalle_Canjes()
        {

            string sConnectionString = Settings.Default.GD1C2013ConnectionString;
            SqlConnection connection = new SqlConnection(sConnectionString);
            SqlCommand cmd = new SqlCommand("THE_BEST_IAS.pr_detalle_canjes", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@p_usu_codigo", SqlDbType.BigInt)).Value = Cliente.clientId;

            //cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.CommandTimeout = 1000;

            connection.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            //int valorRetorno = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);


            //if (valorRetorno == 0)
            //{
            //}

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    grillaCanjes.Rows.Add(new object[] { 
                            dr["can_fecha"].ToString().Trim(), 
                            dr["prem_nombre"].ToString().Trim(), 
                            dr["can_cant"].ToString().Trim(),
                            dr["prem_precio"].ToString().Trim(), 
                    });
                }

                connection.Close();
            }
            else
            {
                label5.Visible = false;
                grillaCanjes.Visible = false;
            }
        }

        private void Cargar_Puntos()
        {

            string sConnectionString = Settings.Default.GD1C2013ConnectionString;
            SqlConnection connection = new SqlConnection(sConnectionString);
            SqlCommand cmd = new SqlCommand("THE_BEST_IAS.pr_consultar_puntos", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@p_usu_codigo", SqlDbType.BigInt)).Value = Cliente.clientId;
            cmd.Parameters.Add(new SqlParameter("@p_fecha_actual", SqlDbType.DateTime)).Value = Fechas.fechaSistema;
            
            //cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.CommandTimeout = 1000;

            connection.Open();
            SqlDataReader dr2 = cmd.ExecuteReader();

            //int valorRetorno = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);

          

            //if (valorRetorno == 0)
            //{
            //}

            if (dr2.HasRows)
            {
                if (dr2.Read())
                {
                    txPuntosTotales.Text = dr2["puntos_totales"].ToString().Trim();
                    txPuntosTotales.Enabled = false;
             
                }
            }
            connection.Close();
        }
   

        private void Cargar_Grilla_Detalle_Puntos()
        {

            string sConnectionString = Settings.Default.GD1C2013ConnectionString;
            SqlConnection connection = new SqlConnection(sConnectionString);
            SqlCommand cmd = new SqlCommand("THE_BEST_IAS.pr_detalle_puntos", connection);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@p_usu_codigo", SqlDbType.BigInt)).Value = Convert.ToInt64(Cliente.clientId);

            //cmd.Parameters.Add("@returnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
            cmd.CommandTimeout = 1000;

            connection.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            //int valorRetorno = Convert.ToInt32(cmd.Parameters["@returnValue"].Value);


            //if (valorRetorno == 0)
            //{
            //}

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    grillaPuntos.Rows.Add(new object[] { 
                            dr["coit_tipo"].ToString().Trim(), 
                            dr["punt_cantidad_total"].ToString().Trim(), 
                            dr["punt_cant_usada"].ToString().Trim(),
                            dr["punt_fecha"].ToString().Trim(), 
                            dr["punt_fecha_venc"].ToString().Trim()
                 
                    });
                }

                connection.Close();
            }
            else {
                txPuntosTotales.Text = "0";
                txPuntosTotales.Enabled = false;
                label4.Visible = false;
                grillaPuntos.Visible = false;
            }

        }

        private void FormConsultarPuntos_Load(object sender, EventArgs e)
        {

        }
    }
}
